package com.elegantmall.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elegantmall.dao.GoodsDao;
import com.elegantmall.dao.SellerDao;
import com.elegantmall.pojo.GoodsPojo;
import com.elegantmall.pojo.SellerPojo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SearchServiceImpl {
    private final GoodsDao goodsDao;
    private final SellerDao sellerDao;

    public SearchServiceImpl(GoodsDao goodsDao, SellerDao sellerDao) {
        this.goodsDao = goodsDao;
        this.sellerDao = sellerDao;
    }

    public List<GoodsPojo> searchGoodsByName(String keywords) {
        if(keywords == null){ return null; }

        QueryWrapper<GoodsPojo> wrapper = new QueryWrapper<>();
        wrapper.like("goods_name", keywords);
//        List<GoodsPojo> test = goodsDao.selectList(wrapper);
//        System.out.println(test);
        return goodsDao.selectList(wrapper);
    }

    public List<SellerPojo> searchSellerByName(String keywords) {
        if(keywords == null){ return null; }

        QueryWrapper<SellerPojo> wrapper = new QueryWrapper<>();
        wrapper.like("seller_name", keywords);
        return sellerDao.selectList(wrapper);
    }

    public List<GoodsPojo> getAllGoods() {
        return goodsDao.selectList(null); // 参数为 null 表示无条件查询
    }

    public List<GoodsPojo> searchGoodsByCategory(String category) {
        QueryWrapper<GoodsPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("category", category);
        return goodsDao.selectList(wrapper);
    }

}
